﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Configuration;
using System.Web.Services.Description;
using System.Web.Services.Discovery;
using System.Web.Services.Protocols;

namespace SMSTester
{
    /// <summary>
    /// Work with Proxy-classes
    /// </summary>
    public class ProxyHandling
    {
        public const string DefaultServiceHost = "Localhost";
        public const string DefaultHostApplicationName = "SkySMSService";
        public const int DefaultNetTcpPort = 9001;
        public const int DefaultHttpPort = 8000;
        public const int DefaultPort = 0;

        public enum ProxyBindingType { Http, Https, NetTcp };
        public const ProxyBindingType DefaultBindingType = ProxyBindingType.Http;


        #region CreateProxy
        /// <summary>
        /// Create new proxy class 
        /// </summary>
        /// <typeparam name="C">Client (svcMessages.Messages)</typeparam>
        /// <typeparam name="T">Interface (svcMessages.IMessages)</typeparam>
        /// <param name="svcHostName">Server (Localhost)</param>
        /// <param name="svcHostApplicationName">Service application (SkySMSService)</param>
        /// <param name="svcServiceName">ServiceName (Messages)</param>
        /// <param name="paramBinding">Connection type (http / net.tcp)</param>
        /// <param name="paramPort">Port(8000/9001) "0"=Default</param>
        /// <returns></returns>
        public static C CreateProxy<C, T>(
                string paramHostName,
                string parmaHostApplicationName,
                string paramServiceName, 
                ProxyBindingType paramBinding,
                int paramPort)
            where T : class
            where C : ClientBase<T>
        {
            Binding b;
            string Prefix;
            if (paramHostName == "")
                paramHostName = DefaultServiceHost;
            if (parmaHostApplicationName == "")
                parmaHostApplicationName = DefaultHostApplicationName;

            if (paramBinding == ProxyBindingType.NetTcp)
            {
                b = new System.ServiceModel.NetTcpBinding();
                (b as NetTcpBinding).MaxBufferSize = 65536000;
                (b as NetTcpBinding).MaxReceivedMessageSize = 65536000;
                (b as NetTcpBinding).ReaderQuotas.MaxArrayLength = 1638400;
                (b as NetTcpBinding).SendTimeout = new TimeSpan(0, 20, 1);
                (b as NetTcpBinding).ReceiveTimeout = new TimeSpan(0, 20, 1);
                Prefix = "net.tcp";
                if (paramPort == 0)
                    paramPort = DefaultNetTcpPort;
            }else
            if (paramBinding == ProxyBindingType.Https)
            {
                b = new System.ServiceModel.BasicHttpBinding();
                (b as BasicHttpBinding).Security.Mode = BasicHttpSecurityMode.Transport;
                (b as BasicHttpBinding).Security.Transport.ClientCredentialType =
                    HttpClientCredentialType.Basic;

                (b as BasicHttpBinding).MaxBufferSize = 65536000;
                (b as BasicHttpBinding).MaxReceivedMessageSize = 65536000;
                (b as BasicHttpBinding).ReaderQuotas.MaxArrayLength = 1638400;
                (b as BasicHttpBinding).SendTimeout = new TimeSpan(0, 20, 1);
                (b as BasicHttpBinding).ReceiveTimeout = new TimeSpan(0, 20, 1);
                Prefix = "https";
            }else
            //if (paramBinding == ProxyBindingType.Http)
            {
                b = new System.ServiceModel.BasicHttpBinding();
                (b as BasicHttpBinding).MaxBufferSize = 65536000;
                (b as BasicHttpBinding).MaxReceivedMessageSize = 65536000;
                (b as BasicHttpBinding).ReaderQuotas.MaxArrayLength = 1638400;
                (b as BasicHttpBinding).SendTimeout = new TimeSpan(0, 20, 1);
                (b as BasicHttpBinding).ReceiveTimeout = new TimeSpan(0, 20, 1);
                Prefix = "http";
                if (paramPort == 0)
                    paramPort = DefaultHttpPort;
            }

            var Address = String.Format(
                "{0}://{1}{2}/{3}/{4}",
                Prefix, 
                paramHostName,
                paramPort==0?"":":"+paramPort.ToString(), 
                parmaHostApplicationName, 
                paramServiceName);
            var ea = new EndpointAddress(Address);
            var Res = (C)Activator.CreateInstance(typeof(C), b, ea);
            Res.ClientCredentials.UserName.UserName = "Anonymus";
            Res.ClientCredentials.UserName.Password = "";
            return Res;
        }
        #endregion


    }
}
